﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeManageService;

namespace WeManageForm
{
    public partial class WeekForm : Form
    {
		public DateTime NowShowTime;
		public BindingSource[] sources = new BindingSource[7];
        public WeekForm()
		{
			InitializeComponent();
            HightlightToday();
			NowShowTime = Global.SundayThisWeek;
            BtnToday.Text = DateTime.Now.ToShortDateString() + "  " + DateTime.Now.DayOfWeek.ToString();
            sources[0] = BSSun;
			sources[1] = BSMon;
			sources[2] = BSTue;
			sources[3] = BSWed;
			sources[4] = BSThu;
			sources[5] = BSFri;
			sources[6] = BSSat;
			LoadData();
			DGVSun.CellDoubleClick += DGVDoubleClick;
			DGVMon.CellDoubleClick += DGVDoubleClick;
			DGVTue.CellDoubleClick += DGVDoubleClick;
			DGVWed.CellDoubleClick += DGVDoubleClick;
			DGVThu.CellDoubleClick += DGVDoubleClick;
			DGVFri.CellDoubleClick += DGVDoubleClick;
			DGVSat.CellDoubleClick += DGVDoubleClick;
		}

        private void reset()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DataGridView)
                {
                    DataGridView d = (DataGridView)c;
                    d.BackgroundColor = Color.LightSkyBlue;
                }
            }
        }
        private void HightlightToday()
        {
            switch ((int)DateTime.Now.DayOfWeek)
			{
				case 0: DGVSun.BackgroundColor = Color.DeepSkyBlue; break;
				case 1: DGVMon.BackgroundColor = Color.DeepSkyBlue; break;
				case 2: DGVTue.BackgroundColor = Color.DeepSkyBlue; break;
				case 3: DGVWed.BackgroundColor = Color.DeepSkyBlue; break;
				case 4: DGVThu.BackgroundColor = Color.DeepSkyBlue; break;
				case 5: DGVFri.BackgroundColor = Color.DeepSkyBlue; break;
				case 6: DGVSat.BackgroundColor = Color.DeepSkyBlue; break;
            }
        }

		private void DGVDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex == -1) return;
			foreach (DataGridViewCell cell in (sender as DataGridView).Rows[e.RowIndex].Cells)
			{
				if(cell.OwningColumn.DataPropertyName == "PlanID")
				{
					Plan plan = PlanManager.GetPlanClone((int)cell.Value);
					PlanForm form = new PlanForm(1, plan);
					form.Closed += (_1, _2) => LoadData();
					form.Show();
				}
			}
		}

        private void LoadData()
        {
			PlanQueryArgs args = new PlanQueryArgs();
			args.QueryTimeType = 1;
			args.PState = 0;
			for(int i = 0; i < 7; i++)
			{
				DateTime ViewTime = NowShowTime.AddDays(i);
				args.MinTime = ViewTime;
				args.MaxTime = ViewTime.AddDays(1).AddSeconds(-1);
				sources[i].DataSource = PlanManager.QueryPlan(args);
			}
			if (NowShowTime == Global.SundayThisWeek) HightlightToday();
			else reset();
		}

        private void BtnLastWeek_Click(object sender, EventArgs e)
        {
            NowShowTime = NowShowTime.AddDays(-7);
			LoadData();
		}

        private void BtnNextWeek_Click(object sender, EventArgs e)
		{
            NowShowTime = NowShowTime.AddDays(7);           
            LoadData();
		}



        private void BtnToday_Click(object sender, EventArgs e)
        {
			NowShowTime = Global.SundayThisWeek;
			LoadData();
        }
    }
}
